package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> getDishPage(int page, int pageSize, String name) {
        log.info("page:{},pageSize:{}", page, pageSize);
        Page<DishDto> dishDtoPage = dishService.getDishPage(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    /**
     * 修改回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable Long id) {
        log.info("id:{}", id);
        DishDto dishDto = dishService.queryDishById(id);
        return R.success(dishDto);
    }

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        log.info("dishDto:{}", dishDto);
        dishService.addDish(dishDto);
        return R.success("菜品添加成功");
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteDish(@RequestParam("ids") List<Long> ids) {
        log.info("ids:{}", ids);
        dishService.deleteDish(ids);
        return R.success("菜品删除成功");
    }

    /**
     * 状态修改
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> dishStatusByStatus(@PathVariable Integer status,@RequestParam("ids") List<Long> ids) {
        log.info("status:{},ids:{}", status, ids);
        dishService.dishStatusByStatus(status, ids);
        if (status == 1) {
            return R.success("启用成功");
        }
        return R.success("禁用成功");
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping

    public R<String> editDish(@RequestBody DishDto dishDto) {
        dishService.editDish(dishDto);
        return R.success("修改成功");
    }

    /**
     * 根据套餐id查询改套餐下的菜品
     *
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> queryDishList(Dish dish) {
        List<DishDto> dishDtoList = dishService.queryDishList(dish);
        return R.success(dishDtoList);
    }
}
